/*
 * Created by 岱青海蓝信息系统(北京)有限公司 on 17-7-7 上午9:47
 * Copyright (c) 2017. All rights reserved.
 */

package com.example.dqhl.jdmetro.fragment.life;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.example.dqhl.jdmetro.R;
import com.example.dqhl.jdmetro.activities.life.LifeDetailActivity;
import com.example.dqhl.jdmetro.adapter.life.LifeCommonAdapter;
import com.example.dqhl.jdmetro.base.BaseFragment;
import com.example.dqhl.jdmetro.bean.life.LifeLearn;
import com.example.dqhl.jdmetro.config.Config;
import com.example.dqhl.jdmetro.utils.Dlog;
import com.example.dqhl.jdmetro.utils.JsonUtils;
import com.example.dqhl.jdmetro.utils.JumpItent;
import com.example.dqhl.jdmetro.utils.recycleview.ConsulationRecycleViewLayoutManager;
import com.google.gson.Gson;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.header.progresslayout.ProgressLayout;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * description:生活汇 公共Fragment
 * autour:guanluocang
 * date:17/7/7 09:49
 */
public class LifeCommonFragment extends BaseFragment {

    @Bind(R.id.life_refresh_layout)
    TwinklingRefreshLayout life_refresh_layout;
    private Context context;
    //    @Bind(R.id.learn_ptr_list)
//    PtrClassicFrameLayout learn_ptr_list;
    private String type = "";
    private Double latitude;
    private Double longitude;
    private String state = "";
    private List<LifeLearn.DataBean> distanceList = new ArrayList<>();
    private List<LifeLearn.DataBean> popularityList = new ArrayList<>();
    private LifeCommonAdapter lifeCommonAdapter;
    @Bind(R.id.lv_learnRecycle)
    RecyclerView lv_learnRecycle;
    private int page = 1;

    @Override
    public View initView(LayoutInflater inflater) {
        rootView = inflater.inflate(R.layout.fragment_life_common, null);
        context = getActivity();
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void initData() {
        lifeCommonAdapter = new LifeCommonAdapter(context);
        lv_learnRecycle.setAdapter(lifeCommonAdapter);
        ConsulationRecycleViewLayoutManager layoutManager = new ConsulationRecycleViewLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        lv_learnRecycle.setLayoutManager(layoutManager);
        lv_learnRecycle.setItemAnimator(new DefaultItemAnimator());
        lv_learnRecycle.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
        lv_learnRecycle.setNestedScrollingEnabled(false);
        ProgressLayout headerView = new ProgressLayout(getActivity());
        life_refresh_layout.setHeaderView(headerView);
        life_refresh_layout.setOverScrollRefreshShow(false);
//        lv_learnRecycle.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
//        lv_learnRecycle.setLoadingMoreProgressStyle(ProgressStyle.Pacman);
//        lv_learnRecycle.setArrowImageView(R.drawable.iconfont_downgrey);
        doData(page);
    }

    @Override
    public void initListener() {
        life_refresh_layout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(final TwinklingRefreshLayout refreshLayout) {
                page = 1;
                doData(page);
                refreshLayout.finishRefreshing();
            }

            @Override
            public void onLoadMore(final TwinklingRefreshLayout refreshLayout) {
                page++;
                loadMoreData(page);
                refreshLayout.finishLoadmore();
            }
        });
    }

    /**
     * 获取数据
     */
    public void doData(int pages) {
        state = getArguments().getString("state");
        type = getArguments().getString("type");
        longitude = getArguments().getDouble("longitude");
        latitude = getArguments().getDouble("latitude");
        Dlog.e(longitude + " state_populatity " + latitude);
        //学玩游购 1 2 3 4
        if (state.equals("distance")) {
            RequestParams params = new RequestParams(Config.life_distence);
            params.addBodyParameter("shop_type", type);
            params.addBodyParameter("latitude", latitude + "");
            params.addBodyParameter("longitude", longitude + "");
            params.addBodyParameter("id_auth", user.getId_auth());
            params.addBodyParameter("page", pages + "");
            x.http().post(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    Dlog.e(result);
                    int errCode = JsonUtils.getErrCode(result);
                    if (errCode == 200) {
                        final LifeLearn lifeLearn = new Gson().fromJson(result, LifeLearn.class);
                        distanceList = lifeLearn.getData();
                        lifeCommonAdapter.refresh_info(distanceList);
                        lifeCommonAdapter.setOnItemClickListener(new LifeCommonAdapter.onItemClickListener() {
                            @Override
                            public void onItemClick(View view, int position) {
                                Bundle bundle = new Bundle();
                                bundle.putString("shop_id", lifeLearn.getData().get(position).getId());
                                bundle.putDouble("latitude", latitude);
                                bundle.putDouble("longitude", longitude);
                                JumpItent.jump(getActivity(), LifeDetailActivity.class, bundle);
                            }
                        });
                    }
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {

                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {

                }
            });
        } else if (state.equals("populatity")) {
            RequestParams params = new RequestParams(Config.life_populatity);
            params.addBodyParameter("shop_type", "1");
            params.addBodyParameter("latitude", latitude + "");
            params.addBodyParameter("longitude", longitude + "");
            params.addBodyParameter("id_auth", user.getId_auth());
            x.http().post(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    Dlog.e(result);
                    int errCode = JsonUtils.getErrCode(result);
                    if (errCode == 200) {
                        final LifeLearn lifeLearn = new Gson().fromJson(result, LifeLearn.class);
                        popularityList = lifeLearn.getData();
                        lifeCommonAdapter.refresh_info(popularityList);
                        lifeCommonAdapter.setOnItemClickListener(new LifeCommonAdapter.onItemClickListener() {
                            @Override
                            public void onItemClick(View view, int position) {
                                Bundle bundle = new Bundle();
                                bundle.putString("shop_id", lifeLearn.getData().get(position).getId());
                                bundle.putString("latitude", latitude + "");
                                bundle.putString("logitude", longitude + "");
                                JumpItent.jump(getActivity(), LifeDetailActivity.class, bundle);
                            }
                        });
                    }
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {

                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {

                }
            });
        }
    }

    /**
     * 获取更多数据
     */
    public void loadMoreData(int pages) {
        state = getArguments().getString("state");
        type = getArguments().getString("type");
        longitude = getArguments().getDouble("longitude");
        latitude = getArguments().getDouble("latitude");
        Dlog.e(longitude + " state_populatity " + latitude);
        //学玩游购 1 2 3 4
        if (state.equals("distance")) {
            RequestParams params = new RequestParams(Config.life_distence);
            params.addBodyParameter("shop_type", type);
            params.addBodyParameter("latitude", latitude + "");
            params.addBodyParameter("longitude", longitude + "");
            params.addBodyParameter("id_auth", user.getId_auth());
            params.addBodyParameter("page", pages + "");
            x.http().post(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    Dlog.e(result);
                    int errCode = JsonUtils.getErrCode(result);
                    if (errCode == 200) {
                        final LifeLearn lifeLearn = new Gson().fromJson(result, LifeLearn.class);
                        distanceList.addAll(lifeLearn.getData());
                        lifeCommonAdapter.refresh_info(distanceList);
                    }
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {

                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {

                }
            });
        } else if (state.equals("populatity")) {
            RequestParams params = new RequestParams(Config.life_populatity);
            params.addBodyParameter("shop_type", "1");
            params.addBodyParameter("latitude", latitude + "");
            params.addBodyParameter("longitude", longitude + "");
            params.addBodyParameter("id_auth", user.getId_auth());
            x.http().post(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    Dlog.e(result);
                    int errCode = JsonUtils.getErrCode(result);
                    if (errCode == 200) {
                        final LifeLearn lifeLearn = new Gson().fromJson(result, LifeLearn.class);
                        popularityList.addAll(lifeLearn.getData());
                        lifeCommonAdapter.refresh_info(popularityList);
                    }
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {

                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {

                }
            });
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
